//
//  ZBScorllView.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "ZBScorllView.h"
#import "FitHeader.h"
#import "UIImageView+WebCache.h"
#import "ZBBScrollViewModel.h"

@implementation ZBScorllView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 * FITWidth, 200 * FITHeight)];
        [self addSubview:_scrollView];
       
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, _scrollView.frame.size.height - 30, _scrollView.frame.size.width, 30)];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        
        [self addSubview:_pageControl];
     
        
        
        
        _scrollView.delegate = self;
    }
    return self;
}

-(void)setImageViewsWithArray:(NSMutableArray *)array{
    _array = [NSMutableArray array];
    _dataSourceArray = [NSMutableArray array];
    for (ZBBScrollViewModel *model in array) {
        [_dataSourceArray addObject:model.pic];
        
        
        [_array addObject:model];
    }
    _scrollView.contentSize = CGSizeMake(375 * FITWidth * (_dataSourceArray.count + 2), 200 * FITHeight);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.userInteractionEnabled = YES;
    _scrollView.contentOffset = CGPointMake(375 * FITWidth, 0);
    _num = _dataSourceArray.count;
    for (NSInteger i = 0; i < _dataSourceArray.count + 2; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.userInteractionEnabled = YES;
        
       

        if (i < 1) {
        imageView.frame = CGRectMake(0, 0, 375 * FITWidth, 200 * FITHeight);
            
            
            [imageView sd_setImageWithURL:[NSURL URLWithString:[_dataSourceArray lastObject] ]placeholderImage:[UIImage imageNamed:@"750*500"]];
            [_scrollView addSubview:imageView];
        }else if (i >= 1 && i < _num + 1){
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jump:)];
            [imageView addGestureRecognizer:tap];
             imageView.tag = 1000 + i;
            //NSLog(@"%@", _array[i]);
            imageView.frame = CGRectMake(375 * FITWidth * i, 0, 375 * FITWidth , 200 * FITHeight);
            [imageView sd_setImageWithURL:[NSURL URLWithString:[_dataSourceArray objectAtIndex:i - 1]] placeholderImage:[UIImage imageNamed:@"750*500"]];
            [_scrollView addSubview:imageView];
            
        }else{
           imageView.frame =CGRectMake(375 * FITWidth * i, 0, 375 * FITWidth, 200 * FITHeight);
            [imageView sd_setImageWithURL:[NSURL URLWithString:[_dataSourceArray firstObject] ] placeholderImage:[UIImage imageNamed:@"750*500"]];
            [_scrollView addSubview:imageView];
            
        }
    }
    _pageControl.numberOfPages = array.count;
    
    if (_dataSourceArray.count == 1) {
        _scrollView.scrollEnabled = NO;
    }
  
   }


-(void)jump:(UITapGestureRecognizer *)tap {
    
    
   
    
    if ([_array[tap.view.tag - 1001] albumId]) {
         [self.delegate takeUrlString:[_array[tap.view.tag - 1001] albumId]];
    }
  
   
    
   
}
-(void)autoShowNextImage
{
    if (_scrollView.contentOffset.x == (_dataSourceArray.count + 1) * self.frame.size.width ) {
        [_scrollView  setContentOffset:CGPointMake(self.frame.size.width, 0) ];
    }
    else
    {
        [_scrollView setContentOffset:CGPointMake(_scrollView.contentOffset.x + self.frame.size.width, 0) animated:YES];
    }

}
-(void)shouldAutoShow:(BOOL)shouldStart
{
    
    if (_dataSourceArray.count != 1) {
        if (shouldStart)  //开启自动翻页
        {
            if (!_timber) {
                _timber = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
                NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
                [runLoop addTimer:_timber forMode:NSRunLoopCommonModes];
               
                
                
            }
        }
        else   //关闭自动翻页
        {
            if (_timber.isValid) {
                [_timber invalidate];
                _timber = nil;
            }
        }
        

    }
    
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    [_timber invalidate];
    _timber = nil;
    _timber = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
        if (scrollView.contentOffset.x >= self.frame.size.width * (_dataSourceArray.count + 1)) {
            [scrollView  setContentOffset:CGPointMake(self.frame.size.width, 0)];
            
        }
        if (scrollView.contentOffset.x <= 0) {
            [scrollView  setContentOffset:CGPointMake(self.frame.size.width * _dataSourceArray.count, 0)];
        }
    _pageControl.currentPage = scrollView.contentOffset.x / self.frame.size.width - 1;
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
